package com.googlecode.yonggemovie.utils;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtils {

	public Connection getConnection() {
		setDBSystemDir();
		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Connection dbConnection = null;
		String strUrl = "jdbc:derby:moviedb;create=true";
		try {
			dbConnection = DriverManager.getConnection(strUrl);
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return dbConnection;

	}

	private void setDBSystemDir() {
		String userHomeDir = System.getProperty("user.home", ".");
		String systemDir = userHomeDir + "/.moviedb";
		File file=new File(systemDir);
		if (!file.exists()) file.mkdirs();
		System.setProperty("derby.system.home", systemDir);
	}
	
	public static void main(String[] args) {
		DbUtils dbUtils=new DbUtils();
		String sql="create table aa(name varchar(30))";
		Connection conn=null;
		try {
			conn=dbUtils.getConnection();
			Statement stmt=conn.createStatement();
			stmt.executeUpdate(sql);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try { conn.close(); } catch (Exception e) {}
		}
		
	}

}
